package client

import (
	"context"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"

	"kop/framework/wrapper"
	"kop/pb"
	"kop/pb/service"
)

var RoleService service.RoleService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	//s.Init(test.Flags()...)

	RoleService = service.NewRoleService("go.micro.srv.role", c)
}

func GetByUserID(userID, serverID int32) *pb.RoleResp {

	var resp, err = RoleService.GetByUserID(context.Background(), &service.GetByUserIDRequest{UserID: userID, ServerID: serverID})
	if err != nil {
		panic(err)
	}
	return resp
}

func GetRole(roleID int32) *pb.Role {

	var resp, err = RoleService.GetRole(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.Role
}

func GetRoles(roleIDs []int32) map[int32]*pb.Role {

	var resp, err = RoleService.GetRoles(context.Background(), &pb.Int32Slice{Slices: roleIDs})
	if err != nil {
		panic(err)
	}

	var m = make(map[int32]*pb.Role, len(resp.Roles))
	for _, v := range resp.Roles {
		m[v.RoleID] = v
	}
	return m
}

func GetVIP(roleID int32) *service.VIP {

	var resp, err = RoleService.GetVIP(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}

	return resp
}

func GetEvents(roleID int32) []int32 {

	var resp, err = RoleService.GetEvents(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.Slices
}

func Event1(roleID, eventID int32) (*service.RoleEventResp, error) {

	return RoleService.Event1(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: eventID})
}

func Event2(roleID, eventID, answer int32) (*service.RoleEventResp, error) {

	return RoleService.Event2(context.Background(), &service.RoleEvent2Request{RoleID: roleID, EventID: eventID, Answer: answer})
}

func Event3(roleID, eventID int32, captain *pb.Captain) (*service.RoleEvent3Resp, error) {

	return RoleService.Event3(context.Background(), &service.RoleEvent3Request{RoleID: roleID, EventID: eventID, Captain: captain})
}

func UpdatePower(roleID int32, power int64) {

	_, _ = RoleService.UpdatePower(context.Background(), &service.Int64Request{RoleID: roleID, Value: power})
}

func GetMaxPower(roleID int32) int64 {

	var resp, err = RoleService.GetMaxPower(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}

	return resp.Value
}

func GetDeclaration(roleID int32) string {

	var resp, err = RoleService.GetDeclaration(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}

	return resp.Value
}

func WorshipDiamond(roleID int32) int32 {

	var resp, err = RoleService.WorshipDiamond(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}

	return resp.Value
}
